var dungeonCon = module.exports
var redisClient = require('../dao/redis/redisClient')

//挑战副本
dungeonCon.challenge = function(player, did) {
    var dungeonId = player.dungeon;
    if (dungeonId !== 0) {
        return 2;
    }
    var dungeon = global.dungeon.get(did);
    if (!dungeon) {
        return 3;
    }
    if (player.lvl < dungeon.lvl) {
        return 4;
    }
    challengeDungeon(player, dungeon);
}

//挑战副本下一关
dungeonCon.challengeNext = function(player) {
    var dungeonId = player.dungeon;
    if (dungeonId === 0) {
        return 2;
    }
    challengeDungeon(player, dungeon);
}

//离开当前的副本
dungeonCon.exitDungeon = function(player) {
    player.progress = 1;
    player.dungeon = 0;
    player.savePlayer()
}

function challengeDungeon(player, dungeon) {
    var progress = player.progress
    var monsterId = dungeon[progress]
    var monster = global.monsters.get(monsterId)
    redisClient.set('u:' + player.userId + ':btime', new Date().getTime())
    var fightRecord = skillAttack.startBattle(player, monster)
    if (fightRecords.results) {
        if (progress === dungeon.num) {
            player.progress = 1;
            player.dungeon = 0;
        } else {
            player.progress += 1;
        }
    }
    player.savePlayer()
    return fightRecord
}